<?php

/**
 * @Author: king
 * @Date:   2019-05-27 22:09:41
 * @Last Modified by:   king
 * @Last Modified time: 2019-06-04 23:29:18
 */
namespace App\Models\Api;
use App\Models\Api\Base;
use App\Exceptions\ApiException;
use Cache;
use DB;
class LanmeiUserAccountRecord extends Base
{
    protected $table = 'user_account_record';
     public $timestamps = false;
    protected $guarded = [];
    /**
     * 获取流水类型 名称
     * @return array
     */
    public static function getType()
    {
        return [
            1 => '问答赏金',
            2 => '赏金退还',
            3 => '围观赏金',
            4 => '提现',
            5 => '围观消费',
            6 => '咨询问题',
            7 => '充值余额',
            8 => '服务消费',
            9 => '余额支付',
            10 => '余额充值',
        ];
    }

    /**
     *
     * @param $accountId
     * @return false|\PDOStatement|string|\think\Collection
     * @throws ApiException
     */
    public static function getList($accountId)
    {
        try
        {
            $data = self::where('account_id', $accountId)
                ->select('id','record_type','price','balance','create_time')
                ->orderBy('create_time', 'desc')->get();
        }
        catch (Exception $e)
        {
            throw new ApiException('服务异常', 200, config('code.error'));
        }

        return $data;
    }

    /**
     * 返回数据格式化
     * @param $data
     * @return array
     */
    public static function dataReset($data)
    {
        $resData = [];
        $type = self::getType();
        foreach ($data as $value)
        {
            $priceType = in_array($value['record_type'], [1, 2, 3]) ? '+' : '-';

            $resData[] = [
                'id' => $value['id'],
                'name' => $type[$value['record_type']],
                'money' => $priceType . $value['price'],
                'balance' => '余额' . $value['balance'],
                'create_time' => date('Y-m-d H:i', $value['create_time'])
            ];
        }
        return $resData;
    }

    /**
     * 获取账户明细 详情
     * @param $rid
     * @return array
     * @throws ApiException
     */
    public static function getInfo($rid)
    {
        try
        {
            $info = self::where('id', $rid)
                ->select('update_time','record_type','price','balance','status','number')
                ->first();

            $type = self::getType();
            $name = '';

            if (in_array($info['record_type'], [1, 2, 3])) {
                $name = '收入';
            } elseif ($info['record_type'] == 10) {
                $name = '余额充值';
            } else {
                $name = '支出';
            }

            $statusName = self::getStatusName($info['record_type'], $info['status']);
        }
        catch (Exception $e)
        {
            throw new ApiException('服务异常', 200, config('code.error'));
        }

        return [
            [
                'name' => $name,
                'value' => $info['price'],
            ],
            [
                'name' => '账户余额',
                'value' => $info['balance'],
            ],
            [
                'name' => '交易类型',
                'value' => $type[$info['record_type']],
            ],
            [
                'name' => '流水号',
                'value' => $info['number'],
            ],
            [
                'name' => '交易时间',
                'value' => date('Y-m-d H:i:s', $info['update_time']),
            ],
            [
                'name' => '状态',
                'value' => $statusName,
            ],
            [
                'name' => '备注',
                'value' => '',
            ],
        ];
    }

    /**
     * 状态名称
     * @param $type
     * @param $status
     * @return mixed
     */
    protected static function getStatusName($type, $status)
    {
        $data = [
            1 => [
                1 => '成功',
                2 => '进行中',
                3 => '失败',
            ],
            2 => [
                1 => '成功',
                2 => '进行中',
                3 => '失败',
            ],
            3 => [
                1 => '成功',
                2 => '进行中',
                3 => '失败',
            ],
            4 => [
                1 => '提现成功',
                2 => '提现中',
                3 => '提现失败',
            ],
            5 => [
                1 => '成功',
                2 => '进行中',
                3 => '失败',
            ],
            6 => [
                1 => '成功',
                2 => '进行中',
                3 => '失败',
            ],
        ];

        return $data[$type][$status];
    }
}
